package com.pe.pgn.clubpgn.dao.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.pe.pgn.clubpgn.common.CLPConstantes;
import com.pe.pgn.clubpgn.dao.UbigeoDao;
import com.pe.pgn.clubpgn.dao.hibernate.GenericDaoHibernate;
import com.pe.pgn.clubpgn.domain.ClpmUbigeo;
import com.pe.pgn.clubpgn.util.CollectionUtil;

@Repository("ubigeoDao")
public class UbigeoDaoImpl extends GenericDaoHibernate<ClpmUbigeo, Long> implements
		UbigeoDao {

	public UbigeoDaoImpl(){
		super(ClpmUbigeo.class);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> obtenerDepartamentos(boolean capitalPrimero){
		
		StringBuffer query = new StringBuffer();
		query.append("select distinct co_departamento as coDepartamento, ");
		query.append("		de_departamento as deDepartamento");
		query.append("	from clpm_ubigeo ");
		query.append("	where st_ubigeo = '").append(CLPConstantes.ST_VERDADERO).append("' ");
		query.append("	order by de_departamento asc");
		
		List<Map<String, Object>> lista = findListOfMaps(query.toString(), null);
		if(capitalPrimero){
			lista = CollectionUtil.setFirstMapFromList(lista, "deDepartamento", CLPConstantes.LIMA);
		}
		return lista;
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> obtenerProvinciasPorDepartamento(boolean capitalPrimero, BigDecimal coDepartamento){
		
		StringBuffer query = new StringBuffer();
		query.append("select distinct co_provincia as coProvincia, ");
		query.append("		de_provincia as deProvincia ");
		query.append("	from clpm_ubigeo ");
		query.append("	where co_departamento = ").append(coDepartamento);
		query.append("		and st_ubigeo = '").append(CLPConstantes.ST_VERDADERO).append("' ");
		query.append("	order by de_provincia asc ");
		
		List<Map<String, Object>> lista = findListOfMaps(query.toString(), null);
		if(capitalPrimero){
			lista = CollectionUtil.setFirstMapFromList(lista, "deProvincia", CLPConstantes.LIMA);
		}		
		
		return lista;
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> obtenerDistritosPorProvincia(BigDecimal coDepartamento, BigDecimal coProvincia){
		
		StringBuffer query = new StringBuffer();
		query.append("select distinct id as id, ");
		query.append("		co_distrito as coDistrito, ");
		query.append("		de_distrito as deDistrito");
		query.append("	from clpm_ubigeo ");
		query.append("	where co_departamento = ").append(coDepartamento);
		query.append("		and co_provincia = ").append(coProvincia);
		query.append("		and st_ubigeo = '").append(CLPConstantes.ST_VERDADERO).append("' ");
		query.append("	order by de_distrito asc ");
		
		List<Map<String, Object>> lista = findListOfMaps(query.toString(), null);
		return lista;
	}
	
	public ClpmUbigeo obtenerUbigeo(Long id){
		
		ClpmUbigeo ubigeo = get(id);
		return ubigeo;
		
	}
}
